﻿using EtityTest;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace YJ_Hall.Controllers
{
    [Route("yjh/[controller]")]
    [ApiController]
    public class TestController : ControllerBase
    {
        private ApiDbContext _context;
        public TestController(ApiDbContext apiDbContext)
        {
            _context = apiDbContext;
        }

        //[HttpGet]
        //public IList<User> Get()
        //{
        //    //ToList   查询 映射到数据库
        //    return _context.Users.ToList();
        //}

        //public string? GetAllUser()
        //{
        //    return _context.Users.Where(w=>w.FirstName=="张").Select(s=>s.FirstName).FirstOrDefault();
        //}

        //public Result GetAllUser()
        //{
        //    var UserName= _context.Users.Where(w => w.FirstName == "张").Select(s => new
        //    {
        //        s.FirstName,
        //        s.LastName
        //    }    
        //    ).FirstOrDefault();

        //    Result result = new Result();
        //    result.Data = UserName;
        //    result.Code = 200;
        //    result.Responsible = "张三";
        //    return result;
        //}

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// ///path:数据参数
        [HttpGet("{Id}")]
        public Result Get(Guid Id)
        {
            Result result = new Result();
            //where:过滤行
            //select：过滤列
            //tolist():筛选一个范围的数据（集合）
            //FirstOrDefault（）：筛选第一个符合条件的数据行（对象）
            //<>:确认数据类型（泛型）
            User? resulrData = _context.Users.Where(w => w.ID == Id).FirstOrDefault<User>();
            if (resulrData==null)
            {
                result.Code = 404;
            }
            else
            {
                result.Code = 200;
                result.Data = resulrData;
            }
                return result;
        }

        //软删除
        //public Result GetTime(Guid id)
        //{
        //    User? user = new User();
            
        //    if (user != null)
        //    {
        //        user.deltime = DateTime.Now;
        //        user = _context.Users.Where(w => w.ID == id).FirstOrDefault();
        //        return user;
        //    }

        //}

    }
}
